<template>
	<div class="system-dept-container layout-padding">
		<div class="system-dept-padding layout-padding-auto layout-padding-view">
			<div class="system-dept-search mb15">
				<el-input v-auth="'sysDept:query'" size="default" v-model="state.tableData.param.name" placeholder="请输入部门名称"
					style="max-width: 180px"> </el-input>
				<el-button v-auth="'sysDept:query'" size="default" @click="getTableData" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button v-auth="'sysDept:add'" size="default" type="success" class="ml10" @click="onOpenAddDept('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增部门
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" row-key="id"
				default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column label="序号" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.$index + 1 }}
					</template>
				</el-table-column>
				<el-table-column prop="name" label="部门名称" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="status" label="部门状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="description" label="部门描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="create_time" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" v-auths="['sysDept:add', 'sysDept:update', 'sysDept:delete']"
					show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'sysDept:add'"
							@click="onOpenAddDept('add', scope.row)">新增</el-button>
						<el-button size="small" text type="primary" v-auth="'sysDept:update'"
							@click="onOpenEditDept('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" v-auth="'sysDept:delete'"
							@click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<DeptDialog ref="deptDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemDept">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDeptApi } from '/@/api/dept/index'

const deptApi = useDeptApi()
// 引入组件
const DeptDialog = defineAsyncComponent(() => import('/@/views/system/dept/dialog.vue'));

// 定义变量内容
const deptDialogRef = ref();
const state = reactive<SysDeptState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageSize:1,
			page:1,
			name: ""
		},
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	deptApi.getAllDepts(state.tableData.param).then((res) => {
		state.tableData.data = res.result;
		state.tableData.total = res.total;
		setTimeout(() => {
			state.tableData.loading = false;
		}, 200);
	})
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 打开新增菜单弹窗
const onOpenAddDept = (type: string, row?: DeptTreeType) => {
	row ? deptDialogRef.value.openDialog(type, row) : deptDialogRef.value.openDialog(type);
};
// 打开编辑菜单弹窗
const onOpenEditDept = (type: string, row: DeptTreeType) => {
	deptDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = (row: DeptTreeType) => {
	ElMessageBox.confirm(`此操作将删除部门：${row.name}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await deleteDept(row.id);
			getTableData();
			ElMessage.success('删除成功');
		})
		.catch(() => { });
};
// 请求删除部门接口
const deleteDept = (id: number) => {
	return deptApi.deleteDept({ ids: [id] })
}
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style lang="scss" scoped>
.system-dept-container {
	.system-dept-padding {
		padding: 15px;

		.el-table {
			flex: 1;
		}
	}
}</style>
